package cn.vcjf.washcar.api.service.controller;


import cn.vcjf.washcar.api.service.entity.ServiceTime;
import cn.vcjf.washcar.api.service.service.ServiceTimeService;
import cn.vcjf.washcar.common.util.DateUtils;
import cn.vcjf.washcar.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/service/serviceTime")
public class ServiceTimeController {
	@Resource
	private ServiceTimeService serviceTimeService;

	@RequestMapping("/addServiceTime")
	@ResponseBody
	public TableResult addServiceTime(ServiceTime serviceTime) {
		serviceTime.preInsert();
		boolean b = serviceTimeService.insert(serviceTime);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateServiceTime")
	@ResponseBody
	public TableResult updateServiceTime(ServiceTime serviceTime) {
		serviceTime.preUpdate();
		boolean b = serviceTimeService.updateById(serviceTime);
		return TableResult.Result(b);
	}

	@RequestMapping("/findServiceTimeOne")
	@ResponseBody
	public TableResult findServiceTimeOne(ServiceTime serviceTime) {

		EntityWrapper<ServiceTime> wrapper = new EntityWrapper<ServiceTime>();

		if (serviceTime.getId() != null) {
			wrapper.eq("id", serviceTime.getId());
		}
		//Todo

		ServiceTime result = serviceTimeService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findServiceTimeList")
	@ResponseBody
	public TableResult findServiceTimeList(ServiceTime serviceTime) throws ParseException {

		EntityWrapper<ServiceTime> wrapper = new EntityWrapper<ServiceTime>();

		if (serviceTime.getId() != null) {
			wrapper.eq("id", serviceTime.getId());
		}
		//Todo
		wrapper.ne("status", 0);
		wrapper.orderBy("date");
		String nowdate = DateUtils.formatDate(new Date(), "HH:mm");
//		wrapper.gt("str_to_date(startTime,'%H:%i')",date);

		Page<ServiceTime> page = new Page<ServiceTime>(serviceTime.getPage(), serviceTime.getLimit());

		List<ServiceTime> result= serviceTimeService.selectPageList(page, wrapper,nowdate);
		page.setRecords(result);

		return TableResult.Result(page);

	}

	@RequestMapping("/deleteServiceTime")
	@ResponseBody
	public TableResult deleteServiceTime(ServiceTime serviceTime) {
		serviceTime.preUpdate();
		boolean b = serviceTimeService.deleteById(serviceTime.getId());
		return TableResult.Result(b);
	}

}
